﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  ServerContext.cs
//    
// Abstract:
//
//  This module implements the ServerContext class.
//  
//--

using System;
using System.Text;

using Microsoft.OfficeCommunicationsServer.Applications.Common;

namespace Microsoft.EnhancedReminders
{
    /// <summary>
    /// Logging context for use by the server. This extends the basic logging context with additional properties relevant to the server.
    /// </summary>
    /// <remarks>
    /// All fields of this class are readonly. This is because the context may be used on multiple threads simultaneously. Therefore,
    /// any thread that wants to change a property of this class should create a new instance of this class specifying the appropriate
    /// new values for the properties.
    /// </remarks>
    [Serializable]
    public class ServerContext : Context
    {
        // XML tags used to format the properties associated with this context when they are written to ETW
        private const string EmailStartTag = "<email>";
        private const string EmailEndTag = "</email>";
        private const string MailItemIdStartTag = "<mailid>";
        private const string MailItemIdEndTag = "</mailid>"; 
        
        private readonly string _emailAddress;
        private readonly string _mailItemId;

        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="emailAddress">E-mail address to be associated with this logging context</param>
        public ServerContext(string emailAddress)
            : base()
        {
            _emailAddress = emailAddress;
        }

        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="emailAddress">E-mail address to be associated with this logging context</param>
        /// <param name="mailItemId">Mailbox item ID to be assoicated with this logging context</param>
        public ServerContext(string emailAddress, string mailItemId)
            : this(emailAddress)
        {
            _mailItemId = mailItemId;
        }

        /// <summary>
        /// The e-mail address associated with this logging context.
        /// </summary>
        public string EmailAddress
        {
            get
            {
                return _emailAddress;
            }
        }

        /// <summary>
        /// The mailbox item ID associated with this logging context.
        /// </summary>
        public string MailItemId
        {
            get
            {
                return _mailItemId;
            }
        }

        /// <summary>
        /// Returns the string representation of the properties encapsulated by this class.
        /// </summary>
        /// <remarks>
        /// This method is used to write the properties encapsulated by this clas to ETW.
        /// </remarks>
        /// <returns>The string representation of the properties encapsulated by this class</returns>
        public override string ExtendedPropertiesToXmlString()
        {
            StringBuilder extendedProperties = new StringBuilder();

            if (!String.IsNullOrEmpty(_emailAddress))
            {
                extendedProperties.AppendFormat("{0}{1}{2}", EmailStartTag, _emailAddress, EmailEndTag);
            }

            if (!String.IsNullOrEmpty(_mailItemId))
            {
                extendedProperties.AppendFormat("{0}{1}{2}", MailItemIdStartTag, _mailItemId, MailItemIdEndTag);
            }

            return extendedProperties.ToString();
        }

        /// <summary>
        /// Renders the contents of this object to a string.
        /// </summary>
        /// <remarks>
        /// This method is used to write the context to the System.Diagnostics.Trace logs. Only the e-mail address (in truncated form) 
        /// is written so that the information can be more easily viewed on one line.
        /// </remarks>
        /// <returns>
        /// A string representation of this object.
        /// </returns>
        public override string ToString()
        {
            return base.ToString() + (String.IsNullOrEmpty(_emailAddress) ? "" : ", User: " +
                _emailAddress.Substring(0, _emailAddress.IndexOf('@')));
        }
    }
}
